'use client';
import { useTranslations } from 'next-intl';
import { useRouter } from 'next/navigation';
import AvaForm from './AvaForm';
// import { columns, data } from './column';
import Layout from '@/components/Layout';
import styles from './index.module.less';
import { Space, Tag, Table, theme,Modal,Button, type TableProps } from 'antd';
import { useState } from 'react';
import MyForm from './component/myForm';
// 类型注解
interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}
// 模拟数据



export default function User() {
  const t = useTranslations('user');
  const { token } = theme.useToken();
  // 弹窗标题
  const [title,setTitle]=useState('添加')
  // 回显内容
  const [items,setItems]=useState({})
  const [data, setData] = useState([
    {
      key: '1',
      name: '徐小夕',
      role: 1,
      desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
      tags: ['前端工程师', 'developer'],
    },
    {
      key: '2',
      name: '张三',
      role: 2,
      desc: '知乎专栏作家',
      tags: ['前端工程师', 'developer', 'Dooring'],
    },
    {
      key: '3',
      name: '李盟',
      role: 2,
      desc: 'Dooring共建者',
      tags: ['后端工程师', 'V6.Dooring', 'Dooring'],
    },
    {
      key: '4',
      name: '王阿明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '5',
      name: '张小明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '6',
      name: 'Tom',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
    {
      key: '7',
      name: 'Json',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
  ]);
  // 删除
  const handleDelete = (key: string) => {
    setData(data.filter(item => item.key !== key));
  };
  const listStyle: React.CSSProperties = {
    background: token.colorFillAlter,
    borderRadius: token.borderRadiusLG,
    padding: 12
  };
  // 弹窗
  const [isModalOpen, setIsModalOpen] = useState(false);

  const showModal = () => {
    setIsModalOpen(true);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  // 表格列配置
  const columns: TableProps<DataType>['columns'] = [
    {
      title: '用户名',
      dataIndex: 'name',
      key: 'name',
      render: (text) => <a>{text}</a>,
      fixed: 'left',
      width: 100
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      render: role => role === 1 ? '超级管理员' : '开发者'
    },
    {
      title: '简介',
      dataIndex: 'desc',
      key: 'desc',
    },
    {
      title: '标签',
      key: 'tags',
      dataIndex: 'tags',
      // render: (_, { tags }) => (
      //   <>
      //     {tags.map((tag) => {
      //       let color = tag.length > 5 ? 'geekblue' : 'green';
      //       if (tag === 'loser') {
      //         color = 'volcano';
      //       }
      //       return (
      //         <Tag color={color} key={tag}>
      //           {tag.toUpperCase()}
      //         </Tag>
      //       );
      //     })}
      //   </>
      // ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <Button type="primary" onClick={()=>{
            showModal()
            setTitle('新增')
            setItems({})
          }}>
           新增
          </Button>
          <Modal title={title} open={isModalOpen}  onCancel={handleCancel} footer={null}>
           <MyForm data={data} onCancel={handleCancel} items={items} title={title}/>
          </Modal>
          <button onClick={()=>{
            showModal()
            setTitle('编辑')
            setItems(record)
          }}>编辑</button>
          <button onClick={() => {
            handleDelete(record.key)
          }}>删除</button>

        </Space>
      ),
    },
  ];

  return (
    <Layout curActive='/user'>
      <main className={styles.userWrap}>
        <div className={styles.content}>
          <AvaForm />
          <div style={listStyle}>
            <h3>{t('userList')}</h3>
            <Table columns={columns} dataSource={data} pagination={{ pageSize: 5 }} scroll={{ x: 1000 }} />
          </div>
        </div>
      </main>
    </Layout>

  );
}
